home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Core / Includes / UFailure.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  4.7 KB  |  167 lines  |  [TEXT/MPS ]

  1. // UFailure.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __UFAILURE__
  5. #define __UFAILURE__
  6.  
  7. // MacApp
  8.  
  9. #ifndef __MACAPPTYPES__
  10. #include "MacAppTypes.h"
  11. #endif
  12.  
  13. // ANSI
  14.  
  15. #ifndef __SETJMP__
  16. #include <setjmp.h>
  17. #endif
  18.  
  19. //----------------------------------------------------------------------------------------
  20. // This macro can be called on any variable to keep it out of a register. It is
  21. // used specifically in exception handling. Kludge to be used on compilers that
  22. // don't support volatile. Best solution is to use C++ exception handling so that
  23. // it becomes the compiler's responsibility to handle register cached variables. But,
  24. // alas, few compilers support this yet (3/95)
  25. // If you are only going to compile your source with a compiler that supports volatile,
  26. // by all means, go ahead and use it (volatile) in your code. But until MacApp requires
  27. // that all compilers support volatile we have to use it ourselves.
  28. //----------------------------------------------------------------------------------------
  29.  
  30. // The new way for functionality
  31. #define MAVolatile(type, id)                type volatile id
  32. #define MAVolatileInit(type, id, value)        type volatile id(value)
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // Max and min error number constants
  36. //----------------------------------------------------------------------------------------
  37.  
  38. const short minErr = -32768;
  39. const short maxErr = 32767;
  40.  
  41.  
  42. //----------------------------------------------------------------------------------------
  43. // FailInfo
  44. //----------------------------------------------------------------------------------------
  45.  
  46. typedef void (*FailureCleanupProc)(void* context);
  47.  
  48. class FailInfo
  49. {
  50. public:
  51.     inline FailInfo();
  52.  
  53. #if qDebug
  54.     ~FailInfo();
  55. #endif
  56.  
  57.     FailInfo* Install();
  58.         // returns this
  59.  
  60.     inline void ReSignal();
  61.  
  62. #if qDebug
  63.     void Success();
  64. #else
  65.     inline void Success();
  66. #endif
  67.     
  68.     void Reset();
  69.         // returns this
  70.  
  71.     void SetCleanupProc(FailureCleanupProc theCleanupProc, void* itsContext);
  72.  
  73.     Boolean HandlerExists() const;
  74.         // Returns TRUE if this FailInfo is linked into the failure handling chain
  75.  
  76.     //----------------------------------------------------------------------------------------
  77.     // data members
  78.     //----------------------------------------------------------------------------------------
  79. public:
  80.     jmp_buf savedState;
  81.     FailInfo* nextInfo;
  82.     FailureCleanupProc cleanupProc;
  83.     void* cleanupContext;
  84.     long message;
  85.     OSErr error;
  86. #if qDebug
  87.     Boolean installed;
  88. #endif
  89.  
  90.     //----------------------------------------------------------------------------------------
  91.     // static data members
  92.     //----------------------------------------------------------------------------------------
  93. public:
  94.     static FailInfo* gTopHandler;
  95. };
  96. typedef struct FailInfo *FailInfoPtr;
  97.  
  98.  
  99. //----------------------------------------------------------------------------------------
  100. // Global macro definitions
  101. //----------------------------------------------------------------------------------------
  102.  
  103. // The Try macro has taken the place of the FailInfo::Try method, for the
  104. // reason that the code has to _always_ be inline.  The decision as to
  105. // whether or not code is inlined is implemented very differently for various
  106. // compilers, and can be affected by options such as symbolics generation and
  107. // level of optimization.  Because this decision is so far out of our control,
  108. // the only real guarantee is to implement Try as a macro…
  109. //
  110. // The old way:
  111. //
  112. //         FailInfo fi;
  113. //         if (fi.Try())
  114. //         {
  115. //            …
  116. //
  117. // The new way:
  118. //         FailInfo fi;
  119. //         Try(fi)
  120. //         {
  121. //            …
  122. //
  123.  
  124. #define Try(f) if (!setjmp(f.Install()->savedState))
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // Global function declarations
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void Assertion(const Boolean condition, const CStr255& description);
  131.  
  132. long BuildMessage(short lowWord, short highWord);
  133.  
  134. void Failure(OSErr error, long message);
  135.  
  136. void FailMemError();
  137.  
  138. void FailResError();
  139.  
  140. void FailNewMessage(OSErr error, long oldMessage, long newMessage);
  141.  
  142. void FailNIL(void* p);
  143.  
  144. void FailNILResource(Handle r);
  145.  
  146. void FailOSErr(OSErr error);
  147.  
  148. void FailOSAError(long error);
  149.  
  150. void ProgramBreak(const CStr255& grievance);
  151.  
  152. void ProgramReport(const CStr255& grievance, const Boolean breakInDebugger);
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // inline member functions
  156. //----------------------------------------------------------------------------------------
  157.  
  158. inline FailInfo::FailInfo() { Reset(); }
  159.  
  160. inline void FailInfo::ReSignal() { ::Failure(error, message); }
  161.  
  162. #if !qDebug
  163. inline void FailInfo::Success() { gTopHandler = nextInfo; }
  164. #endif
  165.  
  166. #endif
  167.